import Notify from '@vant/weapp/notify/notify';
const curd = getApp().curd;

Page({
  data: {
    OPENID: '',
    list: [],
    value: '',
  },

  onLoad() {
    // 公告
    // this.getNotice();
    // 获取 OPENID 初始化数据
    const that = this;
    wx.getStorage({
      key: 'OPENID',
      success(res) {
        that.getRecord(res.data);
        that.setData({
          OPENID: res.data
        });
      },
      fail() {
        that.getOPENID();
      }
    })
  },

  // 获取通知
  getNotice() {
    curd.retrieve('notice', {}, (res) => {
      Notify({
        message: res[0].content,
        color: '#fff',
        background: 'rgba(255, 182, 0, 1)',
        duration: 5000
      });
    })
  },

  // 获取 OPENID
  getOPENID() {
    wx.cloud
      .callFunction({
        name: 'login',
      })
      .then(res => {
        const OPENID = res.result.OPENID;
        const APPID = res.result.APPID;
        this.getRecord(OPENID);
        wx.setStorage({
          key: 'OPENID',
          data: OPENID,
        });
        wx.setStorage({
          key: 'APPID',
          data: APPID,
        });
      })
      .catch(err => {
        console.error(err)
      })
  },

  // 获取待办列表
  getRecord(OPENID) {
    curd.retrieve('list', { _openid: OPENID }, (list) => {
      this.setData({ list });
    });
  },

  // 子组件addTodo添加时触发
  updateList(e) {
    this.getRecord(this.data.OPENID)
  }
})